/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.efounder.core;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;


/**
 * @author meijun
 *
 * 录音播放类
 */
public class VoicePlayer {


	private  Context mContext;
	private  Handler mHandler;
	ImageView voiceIconView;

	MediaPlayer mediaPlayer = null;

	Context activity;

	public static String currentMsgId;


	public static boolean isPlaying = false;

	/**
	 * @param
	 */

	public void stopPlayVoice() {

		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
		}

	}


	public VoicePlayer(Context context, Handler handler){
		this.mContext = context;
		this.mHandler = handler;
	}

	/**
	 * 给前台界面通知消息,message.arg1 =? 0 代表正在准备.
	 * 									1代表文件损坏,
	 * 								 	2 代表开始播放,
	 * 								 	3 代表播放结束.
	 * @param filePath
     */
	public void playVoice(final String filePath) {
		Message message = Message.obtain();
		//防止播放错乱,UI界面不统一
		message.obj = filePath;
		message.arg1 = 0;
		mHandler.sendMessage(message);
		if (!(new File(filePath).exists())) {
			Toast.makeText(mContext, "文件损坏！", Toast.LENGTH_SHORT).show();
			currentMsgId = null;
			Message obtain = Message.obtain();
			obtain.obj = filePath;
			obtain.arg1 = 1;
			mHandler.sendMessage(obtain);
			return;
		}

		AudioManager audioManager = (AudioManager) mContext
				.getSystemService(Context.AUDIO_SERVICE);

		mediaPlayer = new MediaPlayer();

		audioManager.setMode(AudioManager.MODE_NORMAL);
		audioManager.setSpeakerphoneOn(true);
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);

		try {
			mediaPlayer.setDataSource(filePath);
			mediaPlayer.prepare();
			mediaPlayer
					.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

						@Override
						public void onCompletion(MediaPlayer mp) {
							mediaPlayer.release();
							mediaPlayer = null;
							Message obtain = Message.obtain();
							obtain.obj = filePath;
							obtain.arg1 = 3;
							mHandler.sendMessage(obtain);
							if(mListener!=null){
								mListener.playComplete();
							}
						}

					});

			mediaPlayer.start();
			Message obtain = Message.obtain();
			obtain.obj = filePath;
			obtain.arg1 = 2;

			mHandler.sendMessage(obtain);
			showAnimation();


		} catch (Exception e) {
			currentMsgId = null;

		}
	}



	public void resetPlayer(){

		if (mediaPlayer!=null) {
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
				mediaPlayer.reset();
			}
		}

	}
	private void showAnimation() {
	}
	private OnstateChangeListener mListener;
	public void setonStateChangeListener(OnstateChangeListener listener){
		this.mListener = listener;
	}
	public 	interface  OnstateChangeListener {

		/**
		 * 播放完成
		 */
		void playComplete();


	}


}

